﻿//-----------------------------------------------------------------------
// <copyright company="Microsoft Corporation">
//     Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Microsoft.DevDiv.Wpf.Samples
{
    using System;
    using System.Reflection;

    /// <summary>
    /// IProvideValueTargetSetter implementation for clr object PropertyInfo pairs
    /// </summary>
    public sealed class ClrFieldProvideValueTargetSetter<T> : IProvideValueTargetSetter<T>
    {
        public ClrFieldProvideValueTargetSetter(object targetObject, FieldInfo targetProperty)
        {
            if (targetProperty == null)
            {
                throw new ArgumentNullException("targetProperty");
            }

            this.targetObject = targetObject;
            this.targetProperty = targetProperty;
        }

        public void SetValue(T value)
        {
            if (this.targetProperty == null)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            this.targetProperty.SetValue(this.targetObject, value);
        }

        public void Dispose()
        {
            GC.SuppressFinalize(this);
            this.targetObject = null;
            this.targetProperty = null;
        }

        private object targetObject;
        private FieldInfo targetProperty;
    }
}
